/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import persistence.XmlService;

/**
 *
 * @author Matthy & Jelle
 */
public class XmlPanel extends JPanel{

    /**
     * Label.
     */
    private JLabel datumLabel;
    private JLabel resultaatLabel;

    /**
     * Field.
     */
    private JTextField datumField;

    /**
     * Button.
     */
    private JButton genereerXmlButton;

    public XmlPanel() {
    super();
    initialiseerComponenten();
    layoutComponenten();
    initialiseerEventHandlers();
    }

    private void initialiseerComponenten() {
        datumLabel = new JLabel("Datum (dd/MM/yyyy)*");
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        datumField = new JTextField(format.format(new Date()));
        genereerXmlButton = new JButton("Genereer XML");
        resultaatLabel = new JLabel();
    }

    private void layoutComponenten() {
        this.setLayout(new GridLayout(2, 2));

        // Datum
        this.add(this.datumLabel);
        this.add(this.datumField);

        // Submit
        this.add(this.genereerXmlButton);

        // Resultaat
        this.add(this.resultaatLabel);
    }

    private void initialiseerEventHandlers() {
        this.genereerXmlButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Boolean error = false;
                Date datum = new Date();
                if (!datumField.getText().isEmpty()) {
                    try {
                        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                        datum = format.parse(datumField.getText());
                        datumLabel.setForeground(Color.BLACK);
                    } catch (ParseException ex) {
                        error = true;
                        datumLabel.setForeground(Color.RED);
                    }
                } else {
                    error = true;
                    datumLabel.setForeground(Color.RED);
                }
                if (!error) {
                    XmlService xs = new XmlService();
                    resultaatLabel.setText(xs.maakWeekSchema(datum));
                }
            }
        });
    }

}
